package com.example.demo.config;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Map;


@Intercepts(value =
        {
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
        })
@Component
public class MybatisInterceptor implements Interceptor {


    @Override
    public Object intercept (Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        if (invocation.getMethod().getName().equals("query")){
            for (Object arg : args) {
                if (arg instanceof MappedStatement) {
                    MappedStatement mappedStatement = (MappedStatement) arg;
                    Object proceed = invocation.proceed();
                    return proceed;
//                    System.out.println(dataSource);
                }
            }

        }
        return invocation.proceed();
    }
}
